package com.spring.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spring.boot.entity.Province;
import com.spring.boot.mapper.ProvinceMapper;
import com.spring.boot.service.RadarService;
import com.spring.boot.vo.RadarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.spring.boot.utils.MyUtils.getBeforeDay;

@Service
public class RadarServiceImpl implements RadarService {
    @Autowired
    private ProvinceMapper provinceMapper;

    @Override
    public RadarVo getRadarData(String date) {
        if (date.equals("")){
//        时间格式转换器
            SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd");
//        今天的日期
            Date today = new Date();
//        查今天的数据
            QueryWrapper<Province> todayQueryWrapper = new QueryWrapper<>();
            todayQueryWrapper.likeRight("create_time", matter.format(today)).last("limit 1");
            List<Province> todayProvinceList = provinceMapper.selectList(todayQueryWrapper);

//        昨天的日期
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(today);
            calendar.add(Calendar.DATE, -1);
            Date yesterday = calendar.getTime();
//        查询昨天的数据
            QueryWrapper<Province> yesterdayQueryWrapper = new QueryWrapper<>();
            yesterdayQueryWrapper.likeRight("create_time", matter.format(yesterday)).last("limit 1");
            return getYesterdayRadarVo(todayProvinceList, yesterdayQueryWrapper);
        }
        else {
//        查今天的数据
            QueryWrapper<Province> todayQueryWrapper = new QueryWrapper<>();
            todayQueryWrapper.likeRight("create_time", date).last("limit 1");
            List<Province> todayProvinceList = provinceMapper.selectList(todayQueryWrapper);

//        查询昨天的数据
            QueryWrapper<Province> yesterdayQueryWrapper = new QueryWrapper<>();
            yesterdayQueryWrapper.likeRight("create_time", getBeforeDay(date)).last("limit 1");
            return getYesterdayRadarVo(todayProvinceList, yesterdayQueryWrapper);
        }

    }

    private RadarVo getYesterdayRadarVo(List<Province> todayProvinceList, QueryWrapper<Province> yesterdayQueryWrapper) {
        List<Province> yesterdayProvinceList = provinceMapper.selectList(yesterdayQueryWrapper);

        List<Integer> todayValueList = new ArrayList<>();
        List<Integer> yesterdayValueList = new ArrayList<>();

        fun(todayProvinceList, todayValueList);

        fun(yesterdayProvinceList, yesterdayValueList);

        RadarVo radarVo = new RadarVo(todayValueList, yesterdayValueList);
        return radarVo;
    }

    private void fun(List<Province> yesterdayProvinceList, List<Integer> yesterdayValueList) {
        if (yesterdayProvinceList.size() != 0) {
//            新增境外
            String overseasInputRelative = yesterdayProvinceList.get(0).getOverseasInputRelative();
//            新增本土
            String nativeRelative = yesterdayProvinceList.get(0).getNativeRelative();
//            新增确诊
            String confirmedRelative = yesterdayProvinceList.get(0).getConfirmedRelative();
//            新增无症状
            String asymptomaticRelative = yesterdayProvinceList.get(0).getAsymptomaticRelative();

            yesterdayValueList.add(Integer.valueOf(confirmedRelative));
            yesterdayValueList.add(Integer.valueOf(nativeRelative));
            yesterdayValueList.add(Integer.valueOf(overseasInputRelative));
            yesterdayValueList.add(Integer.valueOf(asymptomaticRelative));
        }
    }
}
